<?php
/**
 * @since      File available since Release v1.1
 */
defined('Inbearshop') or exit('Access Invalid!');
define('MYSQL_RESULT_TYPE',1);
class crontabqunfaControl extends Control{
	public function __construct(){
		parent::__construct();
	}
	/*
	 * 群发
	 */
	public function indexOp(){
		
		$model = Model();
		$list = $model->table('qunfa')->where(array('state'=>'0'))->select();
		
		if(!empty($list)){
			foreach($list as $v){				
				$model->table('qunfa')->where(array('id'=>$v['id']))->update(array('state'=>1));
				
				$condition1 = array();
				$condition1['id'] = array('in',$v['auction_setid']);
								
				$auction = $model->table('auction')->where($condition1)->select();

				$auction1 = '';
				$i = '0';
				foreach($auction as $val){
					if($i=='0'){
						$imgurl = BASE_SITE_URL.'/data/upload/shop/auction/'.$val['auction_add_date'].'/'.$val['auction_img1'];
						$auction1.='{
						"title":"'.'【'.$val['seller_name'].'】的新品开拍了'.'",
						"description":"'.'【'.$val['seller_name'].'】的新品开拍了'.'",
						"url":"'.BASE_SITE_URL.'/index.php?act=shop&seller_id='.$val['seller_id'].'",
						"picurl":"'.$imgurl.'"
					},';
					}
						
					$imgurl = BASE_SITE_URL.'/data/upload/shop/auction/'.$val['auction_add_date'].'/'.$val['auction_img1'];
					$auction1.='{
						"title":"'.bearshop_auction_description_substr($val['auction_description']).'",
						"description":"'.$val['auction_description'].'",
						"url":"'.BASE_SITE_URL.'/index.php?act=index&auction_id='.$val['id'].'",
						"picurl":"'.$imgurl.'"
					},';
			
					$i++;
				}
					
				$auction1=substr($auction1, 0,-1);
	
				$auctions = '{
						"articles":['.$auction1.']
					}';
				
				$access_token = $this->getAccessTokenIn();
				$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$access_token}";
				$condition = array();
				$condition['friend.fid'] = $v['user_id'];
				$field = 'user.openid,user.nickname';
				$user = $model->field($field)->table('friend,user')->join('left')->on('friend.uid = user.user_id')->where($condition)->select();
	
				foreach($user as $val1){
					$str= '{
						"touser":"'.$val1['openid'].'",
						"msgtype":"news",
						"news":'.$auctions.'
					}';
					$res = $this->https_request($url,$str);
					$res = json_decode($res,true);
				
					if($res['errcode']=='42001'){
						$access_token = $this->getAccessToken();
						$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$access_token}";
						$this->https_request($url,$str);
					}
				}


			}
		}	

		echo 'success';
	}
	
}